package com.example.demo.WebService.Controller;

import com.example.demo.Data.Approval.ApprovalVo;
import com.example.demo.WebService.Service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class ApprovalController
{
    @Autowired
    ApprovalService approvalService;

    @RequestMapping(value = "/attendance", method = RequestMethod.GET)
    public List<ApprovalVo> getApproval(@RequestBody @Nullable String jsonString)
    {
        if (jsonString == null || jsonString.equals(""))
        {
            return approvalService.getAllApprovals();
        }
        return approvalService.getApprovalByPeriod(new Date(), new Date());
    }

    @RequestMapping(value = "/attendance/{id}")
    public List<ApprovalVo> getApprovalByID(@RequestBody @Nullable String jsonString, @PathVariable String id)
    {
        if (!id.matches("[0-9]+"))
        {
            throw new RuntimeException("Not correct employee ID format.");
        }
        if (jsonString == null || jsonString.equals(""))
        {
            return approvalService.getAllApprovalsByID(Long.parseLong(id));
        }
        else
        {
            return approvalService.getApprovalByPeriodAndID(Long.parseLong(id), new Date(), new Date());
        }
    }
}
